var rootPath = getRootPath();
var existRecord = false;
var oriPhone;

$(function() {
	//滚动条置顶
	$("body").prepend('<input type="text" id="focus" size="1">');
    $("#focus").focus();
    $("#focus").hide();
	var rleId = getUrlParam("rleId");
	$("#rleId").val(rleId);
	
	createCategorySelect(document.getElementById("categoryId"), "1");
	createBsSelect(document.getElementById("bsId"));
	createEtSelect(document.getElementById("etid"));
	$.ajax({
		url : rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type : 'GET',
		async : false,
		success : function(data) {
			createProvSelect(document.getElementById("province"));
			if ('0000' == data.returnCode) {
				var verify = data.returnObject;
				$("#company").val(verify.company);
				$("#address").val(verify.address);
				
				//行业类别
				if(verify.category && verify.category.categoryId != null && verify.category.categoryId != "") {
					$("#categoryId option[value='" + verify.category.categoryId + "'").prop("selected", "selected");
				}
				
				//企业规模
				if (verify.scale && verify.scale.bsId != null && verify.scale.bsId != "") {
					$("#bsId option[value='" + verify.scale.bsId + "']").prop("selected", "selected");
				}
				
				//经济类型
				if(verify.economicType && verify.economicType.etid != null && verify.economicType.etid != "") {
					$("#etid option[value='" + verify.economicType.etid + "']").prop("selected", "selected");
				}
				
				$("#mainBusiness").val(verify.mainBusiness);
				$("#regFund").val(verify.regFund);
				$("#creCode").val(verify.creCode);
				$("#intro").val(verify.intro);
				$("#contact").val(verify.contact);
			/*	$("#job").val(verify.job);*/
				oriPhone = verify.phone;
				$("#phone").val(verify.phone);
				$("#wechat").val(verify.wechat);
				if(verify.crePic != '') {
					existRecord = true;
					$("#fwSpan").html("");
					$("#frontImg").prop("src",rootPath + "/file/viewimg?fileName=" + verify.crePic +"&type=3");
				}
				
				if(verify.province!=null && verify.province!=""){
					$("#province option[value='"+verify.province+"']").prop("selected","selected");
					$("#province").change();
				}
				if(verify.city!=null && verify.city!=""){
					$("#city option[value='"+verify.city+"']").prop("selected","selected");
					$("#city").change();
				}
				if(verify.area!=null && verify.area!=""){
					$("#area option[value='"+verify.area+"']").prop("selected","selected");
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});
	
	$("#pic").uploadPreview({
		Img : "frontImg",
		Width : 100,
		Height : 100,
		addWaterMark : false
	});
	
	$("#pic").change(function() {
		if($(this).val() == '') {
			$("#frontImg").attr("src","");
			$("#fwSpan").removeClass("watermark");
			$("#fwSpan").html("请上传营业执照");
			showErrMsg("crePic", "请上传营业执照");
		} else {
			hideMsg("crePic");
		}
	});
	
	$("#intro").bind("change,keydown,up",function() {
		$(this).val($(this).val().substring(0,800));
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", "realNameAuth.html?rleId=" + rleId + "&creType=" + creType);
	});

	$("#applyAuth").click(function() {
		console.log(123456789)
		if(!checkData()){
			return;
		}
		//前端数据长度校验
		if($("#company").val().length > 50) {
			$("#company").focus();
			showErrMsg("company", "企业名称输入过长,请精简");
			return;
		}
		
		if($("#address").val().length > 50) {
			$("#address").focus();
			showErrMsg("address", "企业地址输入过长,请精简");
			return;
		}
		
		if($("#mainBusiness").val().length > 50) {
			$("#mainBusiness").focus();
			showErrMsg("mainBusiness", "主要业务输入过长,请精简");
			return;
		}
		
		if($("#creCode").val().length > 50) {
			$("#creCode").focus();
			showErrMsg("creCode", "统一社会信用代码输入过长,请精简");
			return;
		}
		
		if($("#intro").val().length > 800) {
			$("#intro").focus();
			showErrMsg("intro", "简介输入太长,请精简");
			return;
		}
		
		if($("#contact").val().length > 50) {
			$("#contact").focus();
			showErrMsg("contact", "联系人输入过长,请精简");
			return;
		}
		
		/*if($("#job").val().length > 50) {
			$("#job").focus();
			showErrMsg("job", "职位输入过长,请精简");
			return;
		}*/
		
		if($("#phone").val().length > 11) {
			$("#phone").focus();
			showErrMsg("phone", "手机号码输入过长");
			return;
		}
		
		if($("#wechat").val().length > 50) {
			$("#wechat").focus();
			showErrMsg("wechat", "微信账号输入过长");
			return;
		}

		showLoading();
		$("#businessAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type : 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功", {icon : 1,time : 1000}, function() {
						parent.$("#content").prop("src","applyResult.html?rleId=" + rleId + "&title=企业认证");
					});
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("error");
			}
		});
		return false;
	});
});

function checkData(){
	if($.trim($("#company").val()) == '') {
		$("#company").focus();
		showErrMsg("company","请输入企业名称");
		return false;
	}
	if($.trim($("#categoryId").val()) == '') {
		$("#categoryId").focus();
		showErrMsg("categoryId","请选择行业类别");
		return false;
	}
	if($("#province").val() == '' || $("#province").val() == 0) {
		$("#province").focus();
		showErrMsg("area","请选择省份");
		return false;
	}
	//台湾,香港,澳门等地暂无数据
	if($("#city option").length > 1 && $("#city").val() == '') {
		$("#city").focus();
		showErrMsg("area","请选择城市");
		return false;
	}
	//全国有5个不设区的地级市
	if($("#area option").length > 1 && $("#area").val() == '') {
		$("#area").focus();
		showErrMsg("area","请选择区");
		return false;
	}
	/*if($("#bsId").val() == '') {
		$("#bsId").focus();
		showErrMsg("scale","请输入企业规模");
		return false;
	}*/
	if($.trim($("#mainBusiness").val()) == '') {
		$("#mainBusiness").focus();
		showErrMsg("mainBusiness","请输入主营业务");
		return false;
	}
	if($.trim($("#creCode").val()) == '') {
		$("#creCode").focus();
		showErrMsg("creCode","请输入统一社会信用代码");
		return false;
	}
	if(!existRecord && $("#pic").val() == '') {
		$("#pic").focus();
		showErrMsg("crePic","请上传营业执照");
		return;
	}
	if($.trim($("#intro").val()) == '') {
		$("#intro").focus();
		showErrMsg("intro","请输入简介");
		return false;
	}
	if($.trim($("#contact").val()) == '') {
		$("#contact").focus();
		showErrMsg("contact","请输入联系人姓名");
		return false;
	}
	/*if($.trim($("#job").val()) == '') {
		$("#job").focus();
		showErrMsg("job","请输入职位");
		return false;
	}*/
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone", "请输入手机号码");
		return false;
	} else {
		if($("#phone").val() != oriPhone
				&& !checkPhone($("#phone").val())) {
			$("#phone").focus();
			showErrMsg("phone","手机号码格式有误");
			return false;
		}
	}
	
	/*if($.trim($("#email").val()) == '') {
		$("#email").focus();
		showErrMsg("email","请输入邮箱地址");
		return false;
	} else {
		if(!checkEmail($("#``").val())) {
			$("#email").focus();
			showErrMsg("email","邮箱格式有误");
			return false;
		}
	}*/
	if($.trim($("#wechat").val()) == '') {
		$("#wechat").focus();
		showErrMsg("wechat","请输入微信账号");
		return false;
	}
	return true;
}